﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SafeHome
{
    public partial class MainFrm : Form
    {
        private Dictionary<string, string> __user;
        private bool _logged, _emergency;
        private string _checkState, _checkLastTime;
        private Timer _timer;
        private BaseNode root;
        public MainFrm()
        {
            InitializeComponent();
            __user = new Dictionary<string, string>();
            _logged = false;
            _emergency = false;
            _checkState = "监控状态:监控中";
            _checkLastTime = "";
            _timer = new Timer();
            _timer.Interval = 1000;
            _timer.Tick += new EventHandler(TimerTick);
            InitializeHome();
        }

        private void InitializeHome()
        {
            BaseNode current, subroot;
            root = new BaseNode("监视器");
            current = root;
            current.Left = new DoorNode("入口门");
            current = current.Left;
            subroot = current;
            current.Right = new DoorNode("客厅-厨房");
            current = current.Right;
            current.Right = new DoorNode("厨房-卫生间");
            current = current.Right;
            current.Right = new DoorNode("客厅-主卧");
            current = current.Right;
            current.Right = new DoorNode("客厅-副卧");
            current = subroot;
            current.Left = new RoomNode("客厅");
            current = current.Left;
            subroot = current;
            current.Right = new RoomNode("厨房");
            current = current.Right;
            current.Right = new RoomNode("卫生间");
            current = current.Right;
            current.Right = new RoomNode("主卧");
            current = current.Right;
            current.Right = new RoomNode("副卧");
            current = subroot;
            current.Left = new WindowNode("厨房窗");
            current = current.Left;
            current.Right = new WindowNode("厕所窗");
            current = current.Right;
            current.Right = new WindowNode("主卧窗");
            current = current.Right;
            current.Right = new WindowNode("副卧窗");
        }

        private void safeHome项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutFormcs aboutFormcs = new AboutFormcs();
            aboutFormcs.Show();
        }

        private void 注册用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RegisterFrm registerFrm = new RegisterFrm();
            registerFrm.ShowDialog();
            if (registerFrm.Username == string.Empty) return;
            __user.Add(registerFrm.Username, registerFrm.Password);
            _logged = true;
            LoginStatus.Text = "登录状态：已登录";
            LoginUsername.Text = "登录名：" + registerFrm.Username;
            SetLogIn();
        }

        private void 登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ErrorFrm errorFrm = new ErrorFrm();
            LogInFrm logInFrm = new LogInFrm();
            logInFrm.ShowDialog() ;
            if (logInFrm.Username == string.Empty) return;
            if (!__user.ContainsKey(logInFrm.Username))
            {
                errorFrm.Content = "该用户不存在!";
                errorFrm.ShowDialog();
                return;
            }
            if (logInFrm.Password != __user[logInFrm.Username])
            {
                errorFrm.Content = "用户名或密码错误!";
                errorFrm.ShowDialog();
                return;
            }
            errorFrm.Content = "登录成功!";
            errorFrm.ShowDialog();
            _logged = true;
            LoginStatus.Text = "登录状态：已登录";
            LoginUsername.Text = "登录名：" + logInFrm.Username;
            SetLogIn();
        }

        private void 用户密码修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ErrorFrm errorFrm = new ErrorFrm();
            UserChangeFrm userChangeFrm = new UserChangeFrm();
            userChangeFrm.ShowDialog();
            if (userChangeFrm.Username == string.Empty) return;
            if (!__user.ContainsKey(userChangeFrm.Username))
            {
                errorFrm.Content = "该用户不存在!";
                errorFrm.ShowDialog();
                return;
            }
            errorFrm.Content = "修改成功!";
            errorFrm.ShowDialog();
            __user[userChangeFrm.Username] = userChangeFrm.Password;
        }

        private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ErrorFrm errorFrm = new ErrorFrm();
            DeleteFrm deleteFrm = new DeleteFrm();
            deleteFrm.ShowDialog();
            if (deleteFrm.Username == string.Empty) return;
            if (!__user.ContainsKey(deleteFrm.Username))
            {
                errorFrm.Content = "该用户不存在!";
                errorFrm.ShowDialog();
                return;
            }
            if (deleteFrm.Password != __user[deleteFrm.Username])
            {
                errorFrm.Content = "用户名或密码错误!";
                errorFrm.ShowDialog();
                return;
            }
            errorFrm.Content = "用户删除成功!";
            errorFrm.ShowDialog();
            __user.Remove(deleteFrm.Username);
            if (LoginUsername.Text == "登录名：" + deleteFrm.Username)
            {
                _logged = false;
                LoginStatus.Text = "登录状态：未登录";
                LoginUsername.Text = string.Empty;
                SetLogOut();
            }
        }

        private void SetLogIn()
        {
            CheckState.Text = _checkState;
            if (!_emergency)
            {
                CheckLastTimeLabel.Text = "上次监测时间:" + DateTime.Now.ToLongTimeString();
                _checkLastTime = CheckLastTimeLabel.Text;
                StateLabel.Visible = true;
                _timer.Start();
            }
            else
            {
                CheckLastTimeLabel.Text = _checkState;
                DoorStateLabel.Visible = true;
                RoomStateLabel.Visible = true;
                WindowStateLabel.Visible = true;
            }
            NodeView.Visible = true;
        }

        private void SetLogOut()
        {
            _timer.Stop();
            _checkState = CheckState.Text;
            _checkLastTime = CheckLastTimeLabel.Text;
            CheckState.Text = "监控状态：未监控";
            CheckLastTimeLabel.Text = string.Empty;
            NodeView.Visible = false;
            StateLabel.Visible = false;
            DoorStateLabel.Visible = false;
            RoomStateLabel.Visible = false;
            WindowStateLabel.Visible = false;
        }

        private void NodeView_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void TimerTick(object sender, EventArgs e)
        {
            int RoomEmergences = 0, 
                DoorEmergences = 0, 
                WindowEmergences = 0;
            List<Emergency> emergencies = root.Check();
            if (emergencies.Count > 0)
            {
                DoorStateLabel.Text = "房门监测器监测到的异常位置有：\n";
                RoomStateLabel.Text = "房间监测器监测到的异常位置有：\n";
                WindowStateLabel.Text = "窗监测器监测到的异常位置有：\n";
                foreach (Emergency emergency in emergencies)
                {
                    switch (emergency.Type)
                    {
                        case EmergencyType.Door:
                            DoorStateLabel.Text += "                               " + emergency.Position.Name + "(" + emergency.Message + ")" + "\n";
                            DoorEmergences += 1;
                            break;
                        case EmergencyType.Room:
                            RoomStateLabel.Text += "                               " + emergency.Position.Name + "(" + emergency.Message + ")" + "\n";
                            RoomEmergences += 1;
                            break;
                        case EmergencyType.Window:
                            WindowStateLabel.Text += "                             " + emergency.Position.Name + "(" + emergency.Message + ")" + "\n";
                            WindowEmergences += 1;
                            break;
                    }
                }
                if (DoorEmergences == 0) DoorStateLabel.Text += "无";
                if (RoomEmergences == 0) RoomStateLabel.Text += "无";
                if (WindowEmergences == 0) WindowStateLabel.Text += "无";
                _timer.Stop();
                StateLabel.Visible = false;
                RoomStateLabel.Visible = true;
                WindowStateLabel.Visible = true;
                DoorStateLabel.Visible = true;
                _emergency = true;
                EmergencyFrm emergencyFrm = new EmergencyFrm();
                emergencyFrm.Location = new Point(1000, 150);
                emergencyFrm.ShowDialog();
            }
            CheckLastTimeLabel.Text = "上次监测时间:" + DateTime.Now.ToLongTimeString();
        }
    }
}
